package com.yy.good.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yy.good.pojo.Goods;
import com.yy.good.mapper.GoodsMapper;
import org.springframework.stereotype.Service;
import com.yy.good.service.GoodsService;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 货物表(Goods)表服务实现类
 *
 * @author yan yi
 * @since 2021-12-04 10:42:14
 * @version 1.0
 */
@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;

    /**
     * 查询所有数据
     * @return  返回所有数据
     */
	@Override
    public Map<String, Object> findAll() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("list", this.goodsMapper.findAll());
        return map;
    }

    /**
     * 分页查询数据
     * @param pageNum 当前页码
     * @param pageSize 页面显示条数
     * @return 对象列表
     */
	@Override
    public Map<String, Object> findByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> goodss = this.goodsMapper.findAll();
        PageInfo<Goods> pageInfo = new PageInfo<>(goodss);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "分页查询成功");
        map.put("data",pageInfo);
        return map;
    }

    /**
     * 新增数据
     * @param goods 实例对象
     * @return 实例对象
     */
	@Override
    public Map<String, Object> insertOne(Goods goods) {
        // UUID.randomUUID()  返回内容：asd21321-ewrewrew213213-123213zsad-123asdasd这样的形态
        this.goodsMapper.insertOne(goods);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增成功");
        return map;
    }

    /**
     * 通过ID更新单条数据
     * @param goods 实例对象
     * @return 实例对象
     */
	@Override
    public Map<String, Object> updateById(Goods goods) {
        this.goodsMapper.updateById(goods);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过主键删除数据
     * @param goodsid 主键
     * @return 是否成功
     */
	@Override
    public Map<String, Object> deleteById(String goodsid) {
        this.goodsMapper.deleteById(goodsid);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200); 
        map.put("msg", "删除成功");
        return map;
    }
}